{% extends "base.html" %}
{% load i18n %}
{% load static %}
{% load icons %}

{% block content %}
<div class="page-wrapper">
    <div class="page-header d-print-none">
        <div class="container-xl">
            <div class="row g-2 align-items-center">
                <div class="col">
                    <div class="page-pretitle">
                        Network
                    </div>
                    <h2 class="page-title">
                        {{ compute.name }}
                    </h2>
                </div>
            </div>
        </div>
    </div>

    <!-- Page body -->
    <div class="page-body">
        <div class="container-xl">
            <div class="card">
                <div class="card-header">
                    <ul class="nav nav-tabs card-header-tabs">
                        <li class="nav-item">
                        <a class="nav-link" href="{% url 'overview' compute.id %}">
                            <span>{% icon 'dashboard' %}&nbsp;&nbsp;{% trans "Overview" %}</span>
                        </a>
                        </li>
                        <li class="nav-item">
                        <a class="nav-link" href="{% url 'instances' compute.id %}">
                            <span>{% icon 'server' %}&nbsp;&nbsp;{% trans "Instances" %}</span>
                        </a>
                        </li>
                        <li class="nav-item">
                        <a class="nav-link" href="{% url 'storages' compute.id %}">
                            <span>{% icon 'hdd-o' %}&nbsp;&nbsp;{% trans "Storages" %}</span>
                        </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="#">
                                <span class="text-azure">{% icon 'sitemap' %}&nbsp;&nbsp;{% trans "Networks" %}</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="{% url 'interfaces' compute.id %}">
                                <span>{% icon 'wifi' %}&nbsp;&nbsp;{% trans "Interfaces" %}</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="{% url 'nwfilters' compute.id %}">
                                <span>{% icon 'filter' %}&nbsp;&nbsp;{% trans "NWFilters" %}</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="{% url 'virtsecrets' compute.id %}">
                                <span>{% icon 'key' %}&nbsp;&nbsp;{% trans "Secrets" %}</span>
                            </a>
                        </li>
                    </ul>
                </div>

                <div class="card-body">
                    <p class="text-muted">
                        <div class="row">
                            <div class="col-12 col-md-4">
                                <div class="card">
                                    {% if state %}
                                        <div class="ribbon bg-teal">Connect</div>
                                    {% else %}
                                        <div class="ribbon bg-red">Disconnect</div>
                                    {% endif %}
                                    <div class="card-body">
                                        <p class="text-muted">
                                            <dl class="row">
                                                <dt class="col-6">{% trans "Network name" %}</dt>
                                                <dd class="col-6">{{ pool }}</dd>
                                                <dt class="col-6">{% trans "Device" %}</dt>
                                                <dd class="col-6">{{ device }}</dd>
                                                <dt class="col-6">{% trans "State" %}</dt>
                                                <dd class="col-6">
                                                    <form action="" method="post" role="form" aria-label="Network start/stop/delete form">{% csrf_token %}
                                                        {% if state == 0 %}
                                                            <a class="btn btn-sm btn-outline-secondary">False</a>
                                                            <input type="submit" class="btn btn-sm btn-success" name="start" value="{% trans "Start" %}">
                                                            <input type="submit" class="btn btn-sm btn-danger" name="delete" value="{% trans "Delete" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
                                                        {% else %}
                                                            <input type="submit" class="btn btn-sm btn-outline-success" name="stop" value="True" onclick="return confirm('{% trans "Are you sure?" %}')">
                                                        {% endif %}
                                                    </form>
                                                </dd>
                                                <dt class="col-6">{% trans "Autostart" %}</dt>
                                                <dd class="col-6">
                                                    <form action="" method="post" role="form" aria-label="Network enable/disable autostart form">{% csrf_token %}
                                                        {% if autostart == 0 %}
                                                            <input type="submit" class="btn btn-sm btn-outline-secondary" name="set_autostart" value="False">
                                                        {% else %}
                                                            <input type="submit" class="btn btn-sm btn-outline-success" name="unset_autostart" onclick="return confirm('{% trans "Are you sure?" %}')" value="True">
                                                        {% endif %}
                                                    </form>
                                                </dd>
                                            </dl>
                                            
                                            <div class="d-grid col-lg-12">
                                                <button class="btn btn-light" role="button" data-bs-toggle="collapse" data-bs-target="#collapseXML" aria-expanded="true" aria-controls="collapseXML">
                                                    <span class="fa fa-chevron-down"></span>
                                                    {% trans 'XML' %}
                                                </button>
                                                <div class="collapse" id="collapseXML">
                                                    <div class="card card-body">
                                                        <form method="post" role="form" aria-label="Network edit with XML form">{% csrf_token %}
                                                            <div id="xmlheight">
                                                                <input type="hidden" name="edit_xml"/>
                                                                <textarea id="edit_editor">{{ xml }}</textarea>
                                                            </div>
                                                            <button type="submit" class="btn btn-primary float-end" name="edit_network">
                                                                {% trans "Edit" %}
                                                            </button>
                                                        </form>
                                                    </div>
                                                </div>
                                            </div>
                                        </p>
                                    </div>
                                </div>
                            </div>

                            <div class="col-12 col-md-4">
                                <div class="card">
                                    {% if state %}
                                        <div class="ribbon bg-teal">{% trans "IPv4 Configuration" %}</div>
                                    {% else %}
                                        <div class="ribbon bg-red">Disconnect</div>
                                    {% endif %}
                                    <div class="card-body">
                                        <p class="text-muted">
                                            <dl class="row">
                                                <dt class="col-6">{% trans "IPv4 Forwarding" %}</dt>
                                                <dd class="col-6">
                                                     {% if net_forward.0 == 'nat' %}
                                                        {% trans "NAT" %}
                                                    {% endif %}
                                                    {% if net_forward.0 == 'route' %}
                                                        {% trans "ROUTE" %}
                                                    {% endif %}
                                                    {% if net_forward.0 == 'bridge' %}
                                                        {% trans "BRIDGE" %}
                                                    {% endif %}
                                                    {% if not net_forward.0 %}
                                                        {% trans "ISOLATE" %}
                                                    {% endif %}
                                                </dd>
                                                <dt class="col-6">{% trans "Network" %}</dt>
                                                <dd class="col-6">{{ ipv4_network|default:"-"  }}</dd>
                                                <dt class="col-6">{% trans "DHCP" %}:</dt>
                                                <dd class="col-6">
                                                    {% if ipv4_dhcp_range_start and ipv4_dhcp_range_end %}
                                                        <span class="text-success">True</span>
                                                    {% else %}
                                                        <span class="text-danger">False</span>
                                                    {% endif %}
                                                </dd>
                                                {% if ipv4_dhcp_range_start and ipv4_dhcp_range_end %}
                                                    {% if state %}
                                                        <dt class="col-6">{% trans "Start" %}</dt>
                                                        <dd class="col-6">{{ ipv4_dhcp_range_start }}</dd>
                                                        <dt class="col-6">{% trans "End" %}</dt>
                                                        <dd class="col-6">{{ ipv4_dhcp_range_end }}</dd>
                                                    {%  else %}
                                                        <form class="col-lg-12 ps-0" method="post" role="form" aria-label="Edit dhcp range form">{% csrf_token %}
                                                            <dt class="col-6">{% trans "Start" %}</dt>
                                                            <dd class="col-6"><input name="range_start" value="{{ ipv4_dhcp_range_start }}"/></dd>
                                                            <dt class="col-6">{% trans "End" %}</dt>
                                                            <dd class="col-6"><input  name="range_end" value="{{ ipv4_dhcp_range_end }}"/></dd>
                                                            <dt class="col-6"></dt>
                                                            <dd class="col-6 ">
                                                                <input type="submit"
                                                                       class="btn btn-primary"
                                                                       value="Apply"
                                                                       name="modify_dhcp_range"
                                                                       title="{% trans "Edit DHCP Range" %}"
                                                                       onclick="return confirm('{% trans "Are you sure?" %}')"/>
                                                            </dd>
                                                            <input hidden name="family" value="ipv4"/>
                                                        </form>
                                                    {% endif %}
                                                {% endif %}
                                            </dl>
                                            {% if ipv4_dhcp_range_start and ipv4_dhcp_range_end %}
                                                {% if state %}
                                                    {% include 'modify_ipv4_fixed_address.html' %}
                                                {% endif %}

                                                <h5 class="page-header">{% trans "IPv4 Fixed Addresses" %}</h5>

                                            {% endif %}
                                            {% if ipv4_fixed_address %}
                                                <div class="d-grid col-12">
                                                    <button class="btn btn-light"  role="button" data-bs-toggle="collapse" data-bs-target="#collapseipv4fix" aria-expanded="true" aria-controls="collapseXML">
                                                        <span class="fa fa-chevron-down"></span>
                                                        {% trans 'Show' %}
                                                    </button>
                                                    <div id="collapseipv4fix" class="collapse">
                                                        <div class="col-4 float-end mt-2">
                                                            <div class="input-group ">
                                                                <input type="text" class="form-control" id="filter_input">
                                                                <input type="button" class="btn btn-secondary" id="filter_button" value="Filter">
                                                                <button type="button" class="btn btn-secondary" id="filter_clear">{% trans 'Clear' %}</button>
                                                            </div>
                                                        </div>

                                                        <table id="ipv4_table" class="table table-hover">
                                                            <thead>
                                                            <tr>
                                                                <th scope="col">{% trans "MAC" %}</th>
                                                                <th scope="col">{% trans "Address" %}</th>
                                                                <th scope="col">{% trans "Name" %}</th>
                                                                <th scope="col">{% trans "Actions" %}</th>
                                                            </tr>
                                                            </thead>
                                                            <tbody>
                                                            {% for fix4 in ipv4_fixed_address %}
                                                                <tr>  
                                                                <form method="post" role="form" aria-label="IPv4 edit/delete form">{% csrf_token %}
                                                                    <td><input class="form-control" value="{{ fix4.mac }}" name="mac" readonly/></td>
                                                                    <td><input class="form-control" value="{{ fix4.ip }}" name="address" /></td>
                                                                    <td><input class="form-control" value="{{ fix4.name }}" name="name" /></td>
                                                                    <td class="text-nowrap">
                                                                        <input hidden name="family" value="ipv4"/>
                                                                        <button type="submit" class="btn btn-sm btn-primary"
                                                                                name="modify_fixed_address"
                                                                                title="{% trans "Edit entry" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
                                                                            <i class="fa fa-save"></i>
                                                                        </button>
                                                                        <button type="submit" class="btn btn-sm btn-danger"
                                                                                name="delete_fixed_address"
                                                                                title="{% trans "Delete entry" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
                                                                            <i class="fa fa-trash"></i>
                                                                        </button>
                                                                    </td>
                                                                </form>
                                                                </tr>
                                                            {% endfor %}
                                                            </tbody>
                                                        </table>
                                                    </div> 
                                                </div>
                                            {% endif %}
                                        </p>
                                    </div>
                                </div>
                            </div>
                            <div class="col-12 col-md-4">
                                <div class="card">
                                    {% if state %}
                                        <div class="ribbon bg-teal">{% trans "IPv6 Configuration" %}</div>
                                    {% else %}
                                        <div class="ribbon bg-red">Disconnect</div>
                                    {% endif %}
                                    <div class="card-body">
                                        <p class="text-muted">
                                            <dl class="row">
                                                <dt class="col-6">{% trans "IPv6 Forwarding" %}</dt>
                                                <dd class="col-6">
                                                    {% if not net_forward.0 %}
                                                        {% trans "ISOLATE" %}
                                                    {% else %}
                                                        {% trans "ROUTE" %}
                                                    {% endif %}
                                                </dd>
                                                <dt class="col-6">{% trans "Network" %}</dt>
                                                <dd class="col-6">{{ ipv6_network|default:"-" }}</dd>        
                                                <dt class="col-6">{% trans "DHCP" %}</dt>
                                                
                                                <dd class="col-6">
                                                    {% if ipv6_dhcp_range_start and ipv6_dhcp_range_end %}
                                                        <span class="text-success">True</span>
                                                    {% else %}
                                                        <span class="text-danger">False</span>
                                                    {% endif %}
                                                </dd>
                                                {% if ipv6_dhcp_range_start and ipv6_dhcp_range_end %}
                                                    {% if state %}
                                                        <dt class="col-6">{% trans "Start" %}</dt>
                                                        <dd class="col-6">{{ ipv6_dhcp_range_start }}</dd>
                                                        <dt class="col-6">{% trans "End" %}</dt>
                                                        <dd class="col-6">{{ ipv6_dhcp_range_end }}</dd>
                                                    {%  else %}
                                                        <form class="col-lg-12 ps-0" method="post" role="form" aria-label="Edit dhcp range form">{% csrf_token %}
                                                            <dt class="col-6">{% trans "Start" %}</dt>
                                                            <dd class="col-6"><input name="range_start" value="{{ ipv6_dhcp_range_start }}"/></dd>
                                                            <dt class="col-6">{% trans "End" %}</dt>
                                                            <dd class="col-6"><input  name="range_end" value="{{ ipv6_dhcp_range_end }}"/></dd>
                                                            <dt class="col-6"></dt>
                                                            <dd class="d-grid col-6">
                                                                <input 
                                                                    type="submit" 
                                                                    class="btn btn-primary" 
                                                                    value="Apply"
                                                                    name="modify_dhcp_range"
                                                                    title="{% trans "Edit DHCP Range" %}" 
                                                                    onclick="return confirm('{% trans "Are you sure?" %}')"/>
                                                            </dd>
                                                            <input hidden name="family" value="ipv6"/>
                                                        </form>
                                                    {% endif %}
                                                {% endif %}
                                                {% if ipv6_dhcp_range_start and ipv6_dhcp_range_end %}
                                                    {% if state %}
                                                        {% include 'modify_ipv6_fixed_address.html' %}
                                                    {% endif %}
                                                    <h5 class="page-header">{% trans "IPv6 Fixed Addresses" %}</h5>
                                                {% endif %}

                                                {% if ipv6_fixed_address %}
                                                    <div class="d-grid col-12">
                                                        <button class="btn btn-light" role="button" data-bs-toggle="collapse" data-bs-target="#collapseipv6fix" aria-expanded="true" aria-controls="collapseipv6fix">
                                                            <span class="fa fa-chevron-down"></span>
                                                            {% trans 'Show' %}
                                                        </button>
                                                        <div id="collapseipv6fix" class="collapse">
                                                            <div class="col-4 float-end mt-2">
                                                                <div class="input-group">
                                                                    <input type="text" class="form-control" id="filter_input_ipv6">
                                                                    <input type="button" class="btn btn-secondary" id="filter_button_ipv6" value="Filter">
                                                                    <button type="button" class="btn btn-secondary" id="filter_clear_ipv6">{% trans 'Clear' %}</button>
                                                                </div>
                                                            </div>
                                                            <table id="ipv6_table" class="table table-hover">
                                                                <thead>
                                                                <tr>
                                                                    <th scope="col">{% trans "ID" %}</th>
                                                                    <th scope="col">{% trans "Address" %}</th>
                                                                    <th scope="col">{% trans "Name" %}</th>
                                                                    <th scope="col">{% trans "Actions" %}</th>
                                                                </tr>
                                                                </thead>
                                                                <tbody>
                                                                {% for fix6 in ipv6_fixed_address %}
                                                                    <tr>
                                                                        <form method="post" role="form" aria-label="IP v6 edit/delete form">{% csrf_token %}
                                                                            <td><input class="form-control" value="{{ fix6.id }}" name="id" readonly/></td>
                                                                            <td><input class="form-control" value="{{ fix6.ip }}" name="address" /></td>
                                                                            <td><input class="form-control" value="{{ fix6.name }}" name="name" /></td>
                                                                            <td class="text-nowrap">
                                                                                <input hidden name="family" value="ipv6"/>
                                                                                <button type="submit" class="btn btn-sm btn-primary"
                                                                                        name="modify_fixed_address"
                                                                                        title="{% trans "Edit entry" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
                                                                                    <span class="fa fa-save"></span>
                                                                                </button>
                                                                                <button type="submit" class="btn btn-sm btn-danger"
                                                                                        name="delete_fixed_address"
                                                                                        title="{% trans "Delete entry" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
                                                                                    <span class="fa fa-trash"></span>
                                                                                </button>
                                                                            </td>
                                                                        </form>
                                                                    </tr>
                                                                {% endfor %}
                                                                </tbody>
                                                            </table>
                                                        </div>
                                                    </div>
                                                {% endif %}
                                            </dl>
                                        
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </p>
                </div>
                <div class="card-body">
                    <p class="text-muted">
                        <div class="row">
                            <div class="col-12">
                                <div class="card">
                                    {% if state %}
                                        <div class="ribbon bg-teal">{% trans "QoS Configuration" %}</div>
                                    {% else %}
                                        <div class="ribbon bg-red">Disconnect</div>
                                    {% endif %}
                                    
                                    <div class="card-body">
                                        <p class="text-muted">
                                            {% if net_forward.0 == 'route' or net_forward.0 == 'nat' or net_forward.0 == 'isolated'  %}
                                                {% if state == 0 and qos.items|length != 2%}
                                                    <form method="post" name="set_qos" role="form" aria-label="Create network qos form">{% csrf_token %}
                                                        {% include 'add_network_qos.html' %}
                                                    </form>
                                                {% endif %}

                                                <div class="col-12">
                                                    <table class="table table-hover">
                                                        <thead>
                                                            <tr>
                                                                <th scope="col">{% trans "Direction" %}</th>
                                                                <th scope="col">{% trans "Average" %}</th>
                                                                <th scope="col">{% trans "Peak" %}</th>
                                                                <th scope="col">{% trans "Burst" %}</th>
                                                                <th scope="col">{% trans "Actions" %}</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                        {% for q, att in qos.items %}
                                                        <form method="post" role="form" aria-label="QoS configuration form">{% csrf_token %}
                                                            <tr>
                                                                <td><label class="col-form-label">{{ q | capfirst }}</label></td>
                                                                <td><input id="qos_average" class="form-control" name="qos_average"
                                                                            value="{{ att.average|default:'' }}"/>
                                                                </td>
                                                                <td><input id="qos_peak" class="form-control" name="qos_peak"
                                                                            value="{{ att.peak|default:'' }}"/>
                                                                </td>
                                                                <td><input id="qos_burst" class="form-control" name="qos_burst"
                                                                            value="{{ att.burst|default:'' }}"/>
                                                                </td>
                                                                <td class="text-nowrap">
                                                                    <input name="qos_direction" value="{{ q }}" hidden/>
                                                                    <button type="submit" class="btn btn-sm btn-primary"
                                                                            name="set_qos" data-bs-toggle="modal"
                                                                            title="{% trans "Edit QoS" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
                                                                        <i class="fa fa-save"></i>
                                                                    </button>
                                                                    <button type="submit" class="btn btn-sm btn-danger"
                                                                            name="unset_qos"
                                                                            title="{% trans "Delete QoS" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
                                                                        <i class="fa fa-trash"></i>
                                                                    </button>
                                                                </td>
                                                            </tr>
                                                        </form>
                                                        {% endfor %}
                                                        </tbody>
                                                    </table>
                                                </div>
                                            {% endif %}
                                        </p>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </p>
                </div>

            </div>
        </div>
    </div>
</div>
<!--block content-->
{% endblock %}
{% block script %}
<script>
    $.expr[':'].Contains = $.expr.createPseudo(function (arg) {
        return function (elem) {
            return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
        };
    });

    $(document).ready(function () {
        // add event button labeled "filter"
        $('#filter_button').click(function (event) {
            // get value
            let filter_val = $('#filter_input').val();
            if (filter_val == '') {
                // show all
                $('#ipv4_table tbody tr').show();
            } else {
                // hide non-matching items
                let row_not_contains4 = $('#ipv4_table tbody tr input:not([value*=\'' + filter_val + '\'])');
                row_not_contains4.closest('tr').hide();
                // show only matches
                let row_contains4 =  $('#ipv4_table tbody tr input[value*=\'' + filter_val + '\']');
                row_contains4.closest('tr').show();
            }
        });
        // add event button labeled "clear"
        $('#filter_clear').click(function (event) {
            $('#filter_input').val('');
            $('#filter_button').click();
        });

        // trigger filter when enter key pressed
        $('#filter_input').keyup(function (event) {
            if (event.keyCode == 13) {
                $('#filter_button').click();
            }
        });

        // add event button labeled "filter"
        $('#filter_button_ipv6').click(function (event) {
            // get value
            let filter_val = $('#filter_input_ipv6').val();
            if (filter_val == '') {
                // show all
                $('#ipv6_table tbody tr').show();
            } else {
                // hide non-matching items
                let row_not_contains6 = $('#ipv6_table tbody tr input:not([value*=\'' + filter_val + '\'])');
                row_not_contains6.closest('tr').hide();
                // show only matches
                let row_contains6 =  $('#ipv6_table tbody tr input[value*=\'' + filter_val + '\']');
                row_contains6.closest('tr').show();
            }
        });
        // add event button labeled "clear"
        $('#filter_clear_ipv6').click(function (event) {
            $('#filter_input_ipv6').val('');
            $('#filter_button_ipv6').click();
        });

        // trigger filter when enter key pressed
        $('#filter_input_ipv6').keyup(function (event) {
            if (event.keyCode == 13) {
                $('#filter_button_ipv6').click();
            }
        });
    });
</script>
<script src="{% static "js/ace/ace.js" %}"></script>
<script>
    var editor = ace.edit("edit_editor");
    editor.getSession().setMode("ace/mode/xml");

    var edit_input = $('input[name="edit_xml"]');
    editor.getSession().on("change", function () {
        edit_input.val(editor.getSession().getValue());
    });
</script>
{% endblock %}